from PyQt5.QtWidgets import QWidget
from PyQt5.Qt import QPixmap, QPainter, QPoint, QPaintEvent, QMouseEvent, QPen,\
    QColor, QSize
from PyQt5.QtCore import Qt
import time
class Point:
	def __init__(self,x,y):
		self.X = x
		self.Y = y

class PaintBoard(QWidget):
    def __init__(self, Parent=None):
        '''
        Constructor
        '''
        super().__init__(Parent)

        self.__InitData()
        self.__InitView()
        
    def __InitData(self):
        
        self.__size = QSize(600,450)
        
        #新建QPixmap作为画板，尺寸为__size
        self.__board = QPixmap(self.__size)
        self.__board.fill(Qt.white) #用白色填充画板
        
        self.__IsEmpty = True #默认为空画板 
        self.EraserMode = False #默认为禁用橡皮擦模式
        
        self.__lastPos = QPoint(0,0)#上一次鼠标位置
        self.__currentPos = QPoint(0,0)#当前的鼠标位置
        self.__drawPos = []
        
        self.__painter = QPainter()#新建绘图工具
        
        self.__thickness = 10       #默认画笔粗细为10px
        self.__penColor = QColor("black")#设置默认画笔颜色为黑色

        self.isRelease = False
        
        # self.__colorList = QColor.colorNames() #获取颜色列表
     
    def __InitView(self):
        #设置界面的尺寸为__size
        self.setFixedSize(self.__size)
        
    def Clear(self):
        #清空画板
        self.__board.fill(Qt.white)
        self.__drawPos = []
        self.update()
        self.__IsEmpty = True
        
    def IsEmpty(self):
        #返回画板是否为空
        return self.__IsEmpty
    
    def ChangePenThickness(self, thickness=10):
        #改变画笔粗细
        self.__thickness = thickness

    def GetContentAsQImage(self):
        #获取画板内容（返回QImage）
        image = self.__board.toImage()
        return image
        
    def GetDrawPos(self):
        DrawPos = self.__drawPos
        return DrawPos
    
    def paintEvent(self, paintEvent):
        #绘图事件
        #绘图时必须使用QPainter的实例，此处为__painter
        #绘图在begin()函数与end()函数间进行
        #begin(param)的参数要指定绘图设备，即把图画在哪里
        #drawPixmap用于绘制QPixmap类型的对象
        self.__painter.begin(self)
        # 0,0为绘图的左上角起点的坐标，__board即要绘制的图
        self.__painter.drawPixmap(0,0,self.__board)
        self.__painter.end()
        
    def mousePressEvent(self, mouseEvent):

        #鼠标按下时，获取鼠标的当前位置保存为上一次位置
        self.__currentPos =  mouseEvent.pos()
        self.__lastPos = self.__currentPos
        
        
        
    def mouseMoveEvent(self, mouseEvent):
        self.__currentPos =  mouseEvent.pos()
        #画线    
        self.__painter.begin(self.__board)
        self.__painter.setPen(QPen(Qt.black,10))
        self.__painter.drawLine(self.__lastPos, self.__currentPos)
        self.__painter.end()
        
        self.__lastPos = self.__currentPos
        self.__drawPos.append(Point(self.__lastPos.x(),self.__lastPos.y()))  #添加位置
                
        self.update() #更新显示
        
    def mouseReleaseEvent(self, mouseEvent):
        self.__IsEmpty = False #画板不再为空
        self.isRelease = True
        # 
